# @Time : 2021/8/5 8:34
# @Author : Li Kunlun
# @Description : 模型参数的延后初始化

from mxnet import init, nd
from mxnet.gluon import nn


# 1、延后初始化
class MyInit(init.Initializer):
    def _init_weight(self, name, data):
        """
        Init dense0_weight (256, 20)
        Init dense1_weight (10, 256)
        """
        print('Init', name, data.shape)
        # 实际的初始化逻辑在此省略了


net = nn.Sequential()
net.add(nn.Dense(256, activation='relu'),
        nn.Dense(10))

net.initialize(init=MyInit())

X = nd.random.uniform(shape=(2, 20))
net(X)

# 2、避免延后初始化
# (1) 对已初始化的模型重新初始化时
"""
Init dense0_weight (256, 20)
Init dense1_weight (10, 256)
"""
net.initialize(init=MyInit(), force_reinit=True)

# (2) 在创建层的时候指定了它的输入个数，使系统不需要额外的信息来推测参数形状
net = nn.Sequential()
net.add(nn.Dense(256, in_units=20, activation='relu'))
net.add(nn.Dense(10, in_units=256))

"""
Init dense2_weight (256, 20)
Init dense3_weight (10, 256)
"""
net.initialize(init=MyInit())